#include "DataProviderImpls.h"
// ## jmlib
//#include "../../jmlib/csv/CsvReader.h"
//#include "../../jmlib/database/OdbcStatementExecutor.h"
#include "jmlib/csv/CsvReader.h"
#include "jmlib/database/OdbcStatementExecutor.h"
// ## c++
#include <sstream>

namespace jm4bbx {

	void CsvDataProvider::loadData(const std::string& file, BoxDataTable* table) {
		jmlib::CsvReader<BoxDataTable, int, std::string> reader;
		reader.readCsv(table, file);
	}

	/// Adapter rozhrania multi typovej tabulky BoxDataTable na DataTarget, 
	/// ktory je pozadovany ako ciel dat vstupujucich z DB.
	class BoxDataTableToDataTargetAdapter : public jmlib::StatementExecutor::DataTarget {
	public:
		BoxDataTableToDataTargetAdapter(BoxDataTable* table) : table_(table) { }
		virtual ~BoxDataTableToDataTargetAdapter() { }

		virtual void addValue(size_t col, int v) override {
			resizeColumns(col);
			table_->getColumn(col)->addValueCell(v);
		}

		virtual void addValue(size_t col, const std::string& v) override {
			resizeColumns(col);
			table_->getColumn(col)->addValueCell(v);
		}
		BoxDataTable* getTable() { return table_; }
	private:
		BoxDataTable* table_;

		void resizeColumns(const size_t col) {
			if (col >= table_->getColumnCount()) {
				table_->addColumns((col + 1) - table_->getColumnCount());
			}
		}
	};

	void SqlDataProvider::loadData(const std::string& statement, BoxDataTable* table) {
		jmlib::OdbcStatementExecutor odbc;
		BoxDataTableToDataTargetAdapter adapter(table);
		odbc.execute(statement, &adapter);
	}


	void HardCodedDataProvider::loadData(const std::string& spec, 
		BoxDataTable* table) {
			std::stringstream stream;
			// Implementacia pomocou parsovania csv.
			if (spec == "A") {
				stream << "1,Adam" << std::endl;
				stream << "2,Brano" << std::endl;
				stream << "3,Brano" << std::endl;

				stream << "4,David" << std::endl;
				stream << "5,Peter" << std::endl;
				stream << "6,Adam" << std::endl;

				stream << "55,Emil" << std::endl;
				stream << "6,Fero";
			}
			else if (spec == "B") {
				stream << "21,Peter" << std::endl;
				stream << "2,Brano" << std::endl;
				stream << "23,Pavol" << std::endl;

				stream << "24,Jano" << std::endl;
				stream << "5,Emil" << std::endl;
				stream << "5,Emil" << std::endl;

				stream << "5,Emil" << std::endl;
				//stream << "6,Fero" << std::endl;
				//stream << "11,Emil" << std::endl;
				stream << "2,Brano";

				//stream << "21\n2\n23\n24\n5\n6\n2\n";
			}
			else if (spec == "C") {
				stream << "2,Brano,2,Brano" << std::endl;
				stream << "2,Brano,2,Brano" << std::endl;
				stream << "5,Peter,21,Peter" << std::endl;
				stream << "6,Fero,6,Fero" << std::endl;
				stream << "6,Fero,6,Fero" << std::endl;
			}
			jmlib::CsvReader<BoxDataTable, int, std::string> reader;
			reader.readCsv(table, stream);

	}

} // namespace jm4bbx 
